<?php
include_once($_SERVER['DOCUMENT_ROOT'] . '/mba/models/DonVi.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/mba/controls/CapNhatDonViController.php');
?>

<script type="text/javascript">


// funtion lay gia tri tu radio button "chon"
    function getValue(id) {
        document.capnhatdonvi.txtmadv.value = document.getElementById("madv" + id).innerHTML;
        document.capnhatdonvi.txttendv.value = document.getElementById("tendv" + id).innerHTML;
        document.capnhatdonvi.txtdiachidv.value = document.getElementById("diachidv" + id).innerHTML;
        document.capnhatdonvi.txtlienhedv.value = document.getElementById("lienhedv" + id).innerHTML;
        document.capnhatdonvi.txttentk.value = document.getElementById("tentk" + id).innerHTML;
        document.capnhatdonvi.txtmatkhau.value = document.getElementById("matkhau" + id).innerHTML;
        document.capnhatdonvi.txtquyen.value = document.getElementById("quyen" + id).innerHTML;
        // chuyen mau cac textfield tendv,diachidv,lienhedv thanh mau trang', va cho phep sua doi thong tin
        document.capnhatdonvi.txtmadv.readOnly = true;
        document.capnhatdonvi.txtmadv.style.backgroundColor = '#cccccc';
        document.capnhatdonvi.txttendv.style.backgroundColor = '#FFFFFF';
        document.capnhatdonvi.txtdiachidv.style.backgroundColor = '#FFFFFF';
        document.capnhatdonvi.txtlienhedv.style.backgroundColor = '#FFFFFF';
        document.capnhatdonvi.txttentk.style.backgroundColor = '#FFFFFF';
        document.capnhatdonvi.txtmatkhau.style.backgroundColor = '#FFFFFF';
        document.capnhatdonvi.btnsuadv.disabled = false;
        document.capnhatdonvi.btnxoadv.disabled = false;
        document.capnhatdonvi.txttendv.readOnly = false;
        document.capnhatdonvi.txtdiachidv.readOnly = false;
        document.capnhatdonvi.txtlienhedv.readOnly = false;
        document.capnhatdonvi.txttentk.readOnly = false;
        document.capnhatdonvi.txtmatkhau.readOnly = false;
        document.capnhatdonvi.txtquyen.disabled = false;


    }

// fuction hien bang xac nhan truoc khi xoa don vi
    function confirmDelete() {
        if (confirm("Bạn có chắc muốn xóa đơn vị này không?")) {
            return true;
        }
        return false;
    }
</script>

<!doctype html>
<html>
    <head>
        <meta charset=utf-8 />
        <title>Cập nhật đơn vị</title>
        <style type="text/css">

        </style><link href="css/style.css" rel="stylesheet" type="text/css">
        <link href="../SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css">
        <script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
    </head>

    <body>
        <div class="bodyStyle">
            <p>
            <form  method="post" enctype="multipart/form-data" name="capnhatdonvi">
                <p><!-- bang hien thi --></br>

                    <!-- lay du lieu tu bang don vi va hien thi -->
                    <input type="file" name="file" id="file">
                    <input type="submit" name="upload" id="upload" value="upload">
                </p>
                <p>
                    <?php
                    $donvi = new DonVi();
                    $result = $donvi->dsDonVi();
                    echo "<table align='center' width='900' border='2' cellspacing='0' cellpadding='0' ";
                    echo "<tr >";
                    echo "<td align='center'>Danh sách Đơn Vị</td>";
                    echo "</tr>";
                    echo "<tr >";
                    echo "<td colspan='3' valign='top'><table width='900' border='2' cellpadding='0' cellspacing='0'>";
                    echo "<tr>";
                    echo "<td width='30' align='center' >STT</td>";
                    echo "<td width='400' align='center' >Mã Đơn vị </td>";
                    echo "<td width='800' align='center' >Tên Đơn vị </td>";
                    echo "<td width='800' align='center' >Địa chỉ đơn vị </td>";
                    echo "<td width='600' align='center' >Thông tin liên hệ </td>";
                    echo "<td width='600' align='center' >Tên tài khoản </td>";
                    echo "<td width='600' align='center' >Mật khẩu </td>";
                    echo "<td width='50' align='center' >Quyền </td>";
                    echo "<td width='50' align='center' >Chọn</td>";
                    echo "</tr>";
                    $num = mysql_num_rows($result);
                    $i = 0;
                    echo "<tbody style='display:none;'>";
                    while ($row = mysql_fetch_array($result)) {
                        $i++;
                        echo "</tbody>";
                        echo "<tr>";
                        echo "<td align='center' >" . $i . "</td>";
                        echo "<td  align='center'><div id='madv$i'>" . $row['MA_DON_VI'] . "</div></td>";
                        echo "<td  align='left'><div id='tendv$i'>" . $row['TEN_DON_VI'] . "</div></td>";
                        echo "<td  align='left'><div id='diachidv$i'>" . $row['DIA_CHI_DV'] . "</div></td>";
                        echo "<td  align='left'><div id='lienhedv$i'>" . $row['LIEN_HE_DV'] . "</div></td>";
                        echo "<td  align='left'><div id='tentk$i'>" . $row['TEN_TK'] . "</div></td>";
                        echo "<td  align='left'><div id='matkhau$i'>" . $row['MAT_KHAU'] . "</div></td>";
                        echo "<td  align='left'><div id='quyen$i'>" . $row['QUYEN'] . "</div></td>";
                        echo "<td  align='center'><input type='radio' name='chkchon' value='$i' onClick='getValue(this.value);' ></td>";
                        echo "</tr>";
                    }
                    echo "</tbody>";

                    echo "<tr>";
                    echo "<td align='left' colspan='9'><span style=' color:#008000'>Tổng số đơn vị: $num";

                    echo "</span></td>";
                    echo "<td align='right' colspan='2'><font color='green'>";
                    echo "</font></td>";
                    echo "</tr>";
                    echo "</table></td>";
                    echo "</tr>";
                    echo "</table>";
                    //}
//}
                    ?>
                    <?php
                    if (isset($_POST['upload'])) {
                        error_reporting(E_ALL ^ E_NOTICE);
                        require_once 'excel_reader.php';
                        $data = new Spreadsheet_Excel_Reader($_FILES["file"]["tmp_name"], true, 'UTF-8');
                        //in ra tieu de cua bang ket qua
                        echo "<table align='center' class='font' width='450' border='0' cellspacing='0' cellpadding='0'>";
                        echo "<tr>";
                        echo "<td class='main_1'><img border='0' src='Libraries/images/tbl_left.gif' width='10' height='25' /></td>";
                        echo "<td align='center' class='main_1' colspan='2'>Danh sách Thực Hiện Upload</td>";
                        echo "<td class='main_1'><p align='right'><img border='0' src='Libraries/images/tbl_right.gif' width='10' height='25' /></p></td>";
                        echo "</tr>";
                        echo "<tr >";

                        echo "<td width='30' align='center' class='main_3'>STT</td>";
                        echo "<td width='50' align='center' class='main_3'>Mã Khoa </td>";
                        echo "<td width='200' align='center' class='main_3'>Tên Khoa </td>";
                        echo "<td width='50' align='center' class='main_3'>Trạng Thái</td>";

                        echo "</tr>";

                        for ($i = 1; $i <= $data->rowcount($sheet_index = 0); $i++) {
                            echo "<tr>";
                            echo "<td align='center' class='main_3'>" . $i . "</td>";
                            echo "<td class= align='center'>" . $data->val($i, 'A') . "</td>";
                            echo "<td class= align='left'>" . $data->val($i, 'B') . "</td>";

                            //echo '<pre>Mã Khoa: '.$data->val($i,'A').' Tên Khoa: '.$data->val($i,'B').'</pre>';
                            $donvi = new DonVi();
                            $madv = $data->val($i, 'A');
                            $tendv = $data->val($i, 'B');
                            $diachidv = $data->val($i, 'B');
                            $lienhedv = $data->val($i, 'B');
                            $tentk = $data->val($i, 'B');
                            $matkhau = $data->val($i, 'B');
                            $quyen = 1;


                            $donvi->setMaDV($madv);
                            $donvi->setTenDV($tendv);
                            $donvi->setMaDV($madv);
                            $donvi->setTenDV($tendv);
                            $donvi->setDiaChiDV($diachidv);
                            $donvi->setLienHeDV($lienhedv);
                            $donvi->setTenTK($tentk);
                            $donvi->setMatKhau($matkhau);
                            $donvi->setQuyen($quyen);

                            if ($donvi->isExits("don_vi", "MA_DON_VI='" . $donvi->getMaDV() . "'") <= 0) {
                                if ($donvi->themDV() > 0) {
                                    echo "<td class='level_1_$ci' align='center'>OK</td></tr>";
                                } else {
                                    echo "<td class='level_1_$ci' align='center'>bị trùng</td></tr>";
                                }
                            } else {
                                //khoa da ton tai
                                echo "<td class='level_1_$ci' align='center'>bị trùng</td></tr>";
                            }
                        }
                        echo "</table><br/><br/>";
                        //include
                    }
                    ?>
                </p>
            </form>
            <!-- het bang -->
        </p>
        <p>&nbsp;</p>
    </div>
</body>
</html>
